home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / IDE / SUBARTIC / RELEASE.ZIP / sub_arctic / lib / v_slider.java < prev    next >
Encoding:
Java Source  |  1996-10-04  |  12.0 KB  |  360 lines

  1. package sub_arctic.lib;
  2.  
  3. import sub_arctic.input.event;
  4. import sub_arctic.input.pressable;
  5. import sub_arctic.input.simple_draggable;
  6. import sub_arctic.input.callback_object;
  7. import sub_arctic.input.int_holder;
  8.  
  9. import sub_arctic.output.loaded_image;
  10.  
  11. import java.awt.Image;
  12.  
  13. /**
  14.  * This class creates a vertical slider with either the default artkit 
  15.  * or other look.  It handles all the input side processing and then uses
  16.  * v_slider_display (from which it is derived) to do the display.
  17.  * 
  18.  * @author Scott Hudson
  19.  */
  20. public class v_slider extends v_slider_display 
  21.   implements pressable, simple_draggable
  22. {
  23.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  24.   
  25.   /** Object we make callbacks to */
  26.   protected callback_object _callback_obj;
  27.  
  28.   /** 
  29.    * Object that this slider makes callbacks to when moved.  There are two
  30.    * callbacks made.  Number 0 is made dynamically as the slider moves, 
  31.    * while number 1 is made only once the slider is released.  In both cases
  32.    * the callback_info contains an Integer object with the value of the slider.
  33.    *
  34.    * @return callback_object the object we make callbacks to
  35.    */
  36.   public callback_object callback_obj() {return _callback_obj;}
  37.  
  38.   /** 
  39.    * Set the callback object that this button makes callbacks to when moved.
  40.    * There are two callbacks made.  Number 0 is made dynamically as the slider
  41.    * moves, while number 1 is made only once the slider is released.  In both 
  42.    * cases the callback_info contains an Integer object with the value of the 
  43.    * slider.
  44.    *
  45.    * @param callback_object cb the new object to which we make callbacks.
  46.    */
  47.   public void set_callback_obj(callback_object cb) {_callback_obj = cb;}
  48.  
  49.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  50.  
  51.   /* Full constructor. Create a new vertical slider (not scrollbar).
  52.    * @param int             xv       x position.
  53.    * @param int             yv       y position.
  54.    * @param int             hv       height in pixels.
  55.    * @param int             minv     minimum slider value.
  56.    * @param int             maxv     maximum slider value.
  57.    * @param int             init_val initial slider value. 
  58.    * @param int             sincv    small increment value (when the up or 
  59.    *                                 down buttons are pushed).
  60.    * @param int             lincv    large increment value (the user clicks in 
  61.    *                                 the thumb area but not on the thumb).
  62.    * @param callback_object call_obj the object which receives the callbacks.
  63.    * @param loaded_image    up_im    image for up button.
  64.    * @param loaded_image    dn_im    image for down button.
  65.    * @param loaded_image    th_im    image for thumb.
  66.    * @param loaded_image    back_pat image for the background pattern.
  67.    */
  68.   public v_slider(
  69.     int xv, int yv, int hv, 
  70.     int minv, int maxv, 
  71.     int init_val,
  72.     int sincv, int lincv,
  73.     callback_object call_obj, 
  74.     loaded_image up_im, loaded_image dn_im, loaded_image th_im, 
  75.     loaded_image back_pat)
  76.     {
  77.       /* let the superclass do most of the work */
  78.       super(xv,yv,hv,minv,maxv,init_val,sincv,lincv,up_im,dn_im,th_im,back_pat);
  79.  
  80.       /* initialize additional locals */
  81.       _callback_obj = call_obj;
  82.     }
  83.  
  84.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  85.  
  86.   /* Create a new vertical slider (not scrollbar) with the default
  87.    * artkit appearance.
  88.    *
  89.    * @param int             xv       x position.
  90.    * @param int             yv       y position.
  91.    * @param int             hv       height in pixels.
  92.    * @param int             minv     minimum slider value.
  93.    * @param int             maxv     maximum slider value.
  94.    * @param int             init_val initial slider value.
  95.    * @param int             sincv    small increment value (when the up or 
  96.    *                                 down buttons are pushed).
  97.    * @param int             lincv    large increment value (the user clicks in 
  98.    *                                 the thumb area but not on the thumb).
  99.    * @param callback_object call_obj the object which receives the callbacks. 
  100.    */
  101.   public v_slider(
  102.     int xv, int yv, int hv, 
  103.     int minv, int maxv, 
  104.     int init_val,
  105.     int sincv, int lincv,
  106.     callback_object call_obj)
  107.     {
  108.       this(xv,yv,hv,minv,maxv,init_val,sincv,lincv,call_obj,
  109.                             null,null,null,null);
  110.     }
  111.  
  112.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  113.  
  114.   /** Constant for dynamic callback (made at each movement) */
  115.   public static final int DYNAMIC_CALLBACK = 0;
  116.  
  117.   /** Constant for static callback (made only at end of movement) */
  118.   public static final int STATIC_CALLBACK = 1;
  119.  
  120.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  121.  
  122.   /**
  123.    * Do a "dynamic" callback. This is callback that gets used
  124.    * while the thumb of the slider is being dragged.
  125.    *
  126.    * @param event evt the event which caused this callback.
  127.    */
  128.   protected void dynamic_callback(event evt)
  129.     {
  130.       if (callback_obj() != null)
  131.     callback_obj().callback(this,evt,DYNAMIC_CALLBACK,new Integer(value()));
  132.     }
  133.  
  134.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  135.  
  136.   /** 
  137.    * Do a "static" callback.   This is the callback when the
  138.    * thumb is "dropped" (and the mouse released). You should use
  139.    * this callback exclusively if you don't want the dragged objects
  140.    * to move as the scrollbar is moved but only when the thumb is
  141.    * released.
  142.    * 
  143.    * @param event evt the event which caused this callback.
  144.    */
  145.   protected void static_callback(event evt)
  146.     {
  147.       if (callback_obj() != null)
  148.     callback_obj().callback(this,evt,STATIC_CALLBACK,new Integer(value()));
  149.     }
  150.  
  151.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  152.  
  153.   /**
  154.    * This function gets called to handle a press of the "up" button.
  155.    * It is here primarily as a hook for subclass writers who might
  156.    * want subclass specific behavior.
  157.    * 
  158.    * @param event evt the button press event
  159.    */
  160.   protected void handle_up_button(event evt) {
  161.     /* do a negative small increment */
  162.     set_value(value() - small_inc());
  163.  
  164.     /* do callback */
  165.     dynamic_callback(evt);
  166.     static_callback(evt);
  167.   }
  168.  
  169.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  170.  
  171.   /**
  172.    * This function gets called to handle a press of the "down" button.
  173.    * It is here primarily as a hook for subclass writers who might
  174.    * want subclass specific behavior.
  175.    * @param event evt the button press event
  176.    */
  177.   protected void handle_down_button(event evt)  {
  178.     /* do a negative small increment */
  179.     set_value(value() + small_inc());
  180.  
  181.     /* do callback */
  182.     dynamic_callback(evt);
  183.     static_callback(evt);
  184.   }
  185.  
  186.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  187.  
  188.   /** 
  189.    * Handle mouse presses in our bounds -- makes us the drag focus 
  190.    * if they click on the thumb. 
  191.    * @param event  evt       the press event (mouse down).
  192.    * @param Object user_info the information that was handed to the 
  193.    *                         pick_collector when this object was deemed to 
  194.    *                         be picked (this value is given to the drag focus 
  195.    *                         agent also, so it will be propagated along to the 
  196.    *                         drag calls below).
  197.    * @return boolean true if we consumed this event, which we will
  198.    */
  199.   public boolean press(event evt, Object user_info)
  200.     {
  201.       int off;
  202.       int_holder holder;
  203.  
  204.       /* in up-button region? */
  205.       if (evt.local_y() <= _up_img.height()) {
  206.     handle_up_button(evt);
  207.     return true;
  208.       }
  209.  
  210.       /* in down-button region? */
  211.       if (evt.local_y() >= h() - _dn_img.height()) {
  212.     handle_down_button(evt);
  213.     return true;
  214.       }
  215.  
  216.       /* compute the offset to the thumb */
  217.       off = thumb_offset();
  218.  
  219.       /* if there is no thumb, bail out now */
  220.       if (off < 0) return false;
  221.       
  222.       /* before thumb? */
  223.       if (evt.local_y() < _up_img.height()+off) {
  224.     /* do a large negative increment */
  225.     set_value(value() - large_inc());
  226.     
  227.     /* do callback */
  228.     dynamic_callback(evt);
  229.     static_callback(evt);
  230.     
  231.     return true;
  232.       }
  233.   
  234.       /* in the thumb? */
  235.       if (evt.local_y() < _up_img.height()+off+_thumb_img.height()) {
  236.     /* make us the drag focus to track the thumb */
  237.     holder=new int_holder();
  238.     manager.simple_drag_focus.set_focus_to(this,evt,holder);
  239.     return true;
  240.       }
  241.   
  242.       /* must be below the thumb, do a large positive increment */
  243.       set_value(value() + large_inc());
  244.       
  245.       /* do callback */
  246.       dynamic_callback(evt);
  247.       static_callback(evt);
  248.   
  249.       return true;
  250.     }
  251.  
  252.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  253.  
  254.   /** 
  255.    * Companion for press() -- ignore here, it never gets called because
  256.    * we become the drag focus.
  257.    *
  258.    * @param event  evt       the release event.
  259.    * @param Object user_info the information that was handed to the 
  260.    *                         pick_collector when this object was deemed to be 
  261.    *                         picked.
  262.    */
  263.   public boolean release(event evt, Object user_info) 
  264.     { 
  265.       return false; 
  266.     }
  267.  
  268.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  269.  
  270.   /** 
  271.    * Start of a drag for the thumb. 
  272.    * @param event  evt       the event that is starting the drag (usually a 
  273.    *                         press).
  274.    * @param Object user_info the object passed to the simple drag_agent.
  275.    */
  276.   public boolean drag_start(event evt, Object user_info)
  277.     {
  278.       int off;
  279.  
  280.       /* remember how far away from top of thumb the event is */
  281.       off = thumb_offset();
  282.       ((int_holder)user_info).value = evt.local_y() - _up_img.height() - off;
  283.       return true;
  284.     }
  285.  
  286.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  287.  
  288.   /** Movement within a drag of the thumb. 
  289.    * @param event  evt       the drag event (mouse move).
  290.    * @param Object user_info the object passed to the simple drag_agent.
  291.    */
  292.   public boolean drag_feedback(event evt, Object user_info)
  293.     {
  294.       int off, slide_range;
  295.       int value_range, old_value;
  296.  
  297.       /* hold on to old value for a second */
  298.       old_value = value();
  299.       
  300.       /* determine new top of thumb */
  301.       off = evt.local_y() - _up_img.height() - ((int_holder)user_info).value;
  302.  
  303.       /* determine new value from this */
  304.       slide_range = h()-_up_img.height()-_dn_img.height()-_thumb_img.height();
  305.       /* ok, if there is no way to move the slider, don't bother doing 
  306.      any more work */
  307.       if (slide_range==0) {
  308.     /* no more work to do */
  309.     return true;
  310.       }
  311.       /* get the range of values and set the value of the slider */
  312.       value_range = max_val() - min_val();
  313.       set_value(min_val() + (off * value_range)/slide_range);
  314.       
  315.       /* if this is a new value, cause a redraw of us and do the callback */
  316.       if (value() != old_value) {
  317.     dynamic_callback(evt);
  318.       }
  319.  
  320.       return true;
  321.     }
  322.                   
  323.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  324.  
  325.   /**
  326.    * End the simple drag of the thumb.
  327.    * @param event  evt       the end of the drag event (mouse release usually).
  328.    * @param Object user_info the object passed to the simple drag_agent.
  329.    */
  330.   public boolean drag_end(event evt, Object user_info)
  331.     {
  332.       /* let drag_feedback do most of the work */
  333.       drag_feedback(evt, user_info);
  334.  
  335.       /* do the static callback */
  336.       static_callback(evt);
  337.  
  338.       return true;
  339.     }
  340.                  
  341.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  342.  
  343. }
  344. /*=========================== COPYRIGHT NOTICE ===========================
  345.  
  346. This file is part of the subArctic user interface toolkit.
  347.  
  348. Copyright (c) 1996 Scott Hudson and Ian Smith
  349. All rights reserved.
  350.  
  351. The subArctic system is freely available for most uses under the terms
  352. and conditions described in 
  353.   http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html 
  354. and appearing in full in the lib/interactor.java source file.
  355.  
  356. The current release and additional information about this software can be 
  357. found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
  358.  
  359. ========================================================================*/
  360.